package com.xja.controller;/**
 * @className: AdminController
 * @description: TODO 类描述
 * @author: wkx
 * @date: 2022/4/17
 **/

import com.xja.pojo.*;
import com.xja.service.ArticleService;
import com.xja.service.ContentTypeService;
import com.xja.service.UserService;
import com.xja.util.FileUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.FileNotFoundException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName : AdminController  //类名

 * @Author : Lenovo //作者
 * @Date: 2022/4/17  10:52
 */
@Controller
@RequestMapping("article")
public class ArticleController {
    private Logger logger = LoggerFactory.getLogger(ArticleController.class);
    @Reference(url = "dubbo://localhost:20883")
    private ContentTypeService contentTypeService;
    @Reference(url = "dubbo://localhost:20883")
    private ArticleService articleService;
    @Reference(url = "dubbo://localhost:20881")
    private UserService userService;

    /*
    *@作者:褚
    *@方法:查询文章类别
    *@创建时间:2022/4/17 20:38
    **/
    @RequestMapping("toAddArticle")
    private String toAddBlog(Model model){
        logger.info("运行查询文章类别的方法");
        List<ContentType> articleTypeList = contentTypeService.selectConType();
        model.addAttribute("articleTypeList",articleTypeList);
        return "article-add";
    }
    /*
    *@作者:褚
    *@方法:添加文章信息
    *@创建时间:2022/4/17 20:39
    **/
    @RequestMapping("addArticle")
    private String addArticle(HttpSession session, Article article, MultipartFile file){
        logger.info("添加文章信息");
        BlogUsers user = (BlogUsers) session.getAttribute("user");
        article.setBloguid(user.getUid());
        String path = null;
        try {
            path = FileUtil.getTransferToPath(file);
            article.setArtimg(path);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        long time = new Date().getTime();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy:MM:dd");
        String format = dateFormat.format(time);
        article.setCreatetime(format);
        articleService.addArticle(article);
        return "redirect:/blog/getIndex";
    }
    /*
    *@作者:褚
    *@方法:查询个人所有信息
    *@创建时间:2022/4/18 11:21
    **/
    @RequestMapping("personalMessage")
    private String personalMessage(HttpServletRequest request,@RequestParam(value = "astatus",defaultValue = "-1")Integer astatus , @RequestParam(value = "count",defaultValue = "0")Integer count ,  Model model){
        logger.info("查询个人所有信息");
        BlogUsers user = (BlogUsers) request.getSession().getAttribute("user");
        count = count+3;
        List<Article> articleList = articleService.selectArticleByUid(user.getUid(),count,astatus);//文章信息
        if (astatus==5){
            Map<Integer, String> rejectArtMap = new HashMap<>();
            for (Article article:articleList){
                ArticleCheckMessage rejectArt = articleService.getArticleMessage(article.getAid());//被驳回的原因
                rejectArtMap.put(article.getAid(),rejectArt.getTitle());
            }
            model.addAttribute("rejectArtMap",rejectArtMap);
        }
        Integer articleCount = articleService.selectArticleCount(user.getUid());//文章总数
        List<FocusInfo> targetList = userService.selectFocusByUid(user.getUid());//关注信息
        Integer sourceCount = userService.selectSourceCountByUid(user.getUid());//粉丝总数
        Integer collnum = articleService.selectCollArtNum(user.getUid());//收藏数
        model.addAttribute("articleList",articleList);
        request.getSession().setAttribute("articleCount",articleCount);
        request.getSession().setAttribute("focusCount",targetList.size());
        request.getSession().setAttribute("sourceCount",sourceCount);
        request.getSession().setAttribute("collnum",collnum);
        model.addAttribute("count",count);
        model.addAttribute("astatus",astatus);
        return "collection";
    }
    /*
    *@作者:褚
    *@方法:根据文章id查询出文章详情并回显在修改页
    *@创建时间:2022/4/21 9:52
    **/
    @RequestMapping("checkReject")
    private String checkReject(Model model,Integer aid){
        logger.info("询出文章详情并回显在修改页");
        Article article = articleService.getArticleByAid(aid);
        List<ContentType> articleTypeList = contentTypeService.selectConType();
        model.addAttribute("articleTypeList",articleTypeList);
        model.addAttribute("article",article);
        return "article-update";
    }
    /*
    *@作者:褚
    *@方法:修改文章信息
    *@创建时间:2022/4/21 13:53
    **/
    @RequestMapping("updateArticle")
    private String updateArticle(Article article,MultipartFile file){
        logger.info("修改文章信息");
        String path = null;
        try {
            path = FileUtil.getTransferToPath(file);
            article.setArtimg(path);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        long time = new Date().getTime();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy:MM:dd");
        String format = dateFormat.format(time);
        article.setCreatetime(format);
        articleService.updateArticle(article);
        return "redirect:/article/personalMessage";
    }
}
